package com.dao.impl;

import com.dao.UserLoginDao;
import com.pojo.Users;
import com.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserLoginDaoImpl implements UserLoginDao {

    //根据用户名和用户密码查询用户信息
    @Override
    public Users selectUsersByUserNameAndUserPwd(String username, String userpwd) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        Users users=null;
        try {
            connection= JDBCUtil.getConnection();
            String sql="select * from users where username=? and userpwd=?";
            preparedStatement= connection.prepareStatement(sql);
            //执行sql语句
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,userpwd);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                users = new Users();
                users.setUsersex(resultSet.getString("usersex"));
                users.setUsername(resultSet.getString("username"));
                users.setUserpwd(resultSet.getString("userpwd"));
                users.setUserid(resultSet.getInt("userid"));
                users.setPhonenumber(resultSet.getString("phonenumber"));
                users.setQqnumber(resultSet.getString("qqnumber"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtil.closeConnection(connection);
        }
        return users;
    }
}
